package com.itheima.controller;


import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConst;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.utils.SMSUtils;
import com.itheima.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

@RestController
@RequestMapping("/validateCode")
@Slf4j
public class ValidateCodeController {

    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("/send4Login")
    public Result send4Login(String telephone){
        log.debug("ValidateCodeController: send4Login: telephone ===> " + telephone);
        Integer validateCode = ValidateCodeUtils.generateValidateCode(6);
        log.debug("ValidateCodeController: send4Login: validateCode ===> " + validateCode);
        try {
            //SMSUtils.sendShortMessage("SMS_184625400",telephone,validateCode.toString());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.SEND_VALIDATECODE_FAIL);
        }
        System.out.println("发送的手机验证码为：" + validateCode);
        jedisPool.getResource().setex(RedisMessageConstant.SENDTYPE_LOGIN + "-" + telephone,5*60,validateCode.toString());

        return new Result(true,MessageConst.SEND_VALIDATECODE_SUCCESS);
    }

    @RequestMapping("/send4Order")
    public Result send4Order(String telephone){
        log.debug("ValidateCodeController: send4Order: telephone ===> " + telephone);
        Integer validateCode = ValidateCodeUtils.generateValidateCode(6);
        log.debug("ValidateCodeController: send4Order: validateCode ===> " + validateCode);
        try {
            //SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,validateCode.toString());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.SEND_VALIDATECODE_FAIL);
        }
        System.out.println("发送的手机验证码为: " + validateCode);
        jedisPool.getResource().setex(RedisMessageConstant.SENDTYPE_ORDER + "-" + telephone,5*60,validateCode.toString());
        return new Result(true,MessageConst.SEND_VALIDATECODE_SUCCESS);
    }

}
